//
//  FineInfoController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/7/11.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
class FineInfoController: BaseController {
    var fineInfo:CarFineModel!
    var fineInfoTable:UITableView!//数据表格
    var fineInfoCell:FineInfoCell!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupNavBar(title: "代缴罚款单", isback: true, appearWay: 3, isright: false)//自定义导航
        if reachability.isReachable {
            getFineInfo()
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
        //初始化表格
        fineInfoTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH,width:kScreenW,height:kScreenH - kStatusBarH - kNavigationBarH),style: UITableViewStyle.plain)
        fineInfoTable?.delegate = self
        fineInfoTable?.dataSource = self
        fineInfoTable?.separatorStyle = UITableViewCellSeparatorStyle.none
        if "1" == fineInfo.isDocument {
          fineInfoTable?.rowHeight = 5*cellH + 6*oTop + remarkH + 2*oImgH + 10
        }else {
            fineInfoTable?.rowHeight = 5*cellH + 7*oTop + remarkH + 2*oImgH + 10
        }
        self.view.addSubview(fineInfoTable)
    }
}
extension FineInfoController{
    //查询审车详情
    func getFineInfo(){
        let urlString = "\(projectUrl)/order/other/carfine/\(fineInfo.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
        Alamofire.request(urlString, method: .get, parameters: nil, headers : headers).responseJSON { (response) in
            if response.result.isSuccess{
                let json = JSON(response.result.value as! [String : AnyObject])
                if json["success"] == true {
                    self.fineInfo = CarFineModel.init(dict: json["rdata"])
                    self.fineInfoTable.reloadData()
                }else{
                    self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                }
            }else{
                print(response.result.error!) //输出错误日志
            }
        }
    }
    //选择操作方式 支付，抢单，完成订单？
    func operatorFun(sender: UIButton){
        if 0 == sender.tag {//去支付
            let pc = PayController()
            pc.orderID = fineInfo.orderNo
            if fineInfo.price.isEmpty || fineInfo.samount.isEmpty {
                checkAlertFun(message: "暂时还不能支付！")
            }else{
                pc.totalAmount = String(describing: CGFloat(Double(fineInfo.price)!) + CGFloat(Double(fineInfo.latePrice)!) + CGFloat(Double(fineInfo.samount)!))
                pc.modelType = "carfine"
                self.present(pc, animated: true, completion: nil)
            }
        }else if(1 == sender.tag){//去抢单
            let urlString = "\(projectUrl)/order/other/accept/carfine/\(fineInfo.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
            toOperatOrder(url: urlString)
        }
        else if(2 == sender.tag){//去完成订单
            let urlString = "\(projectUrl)/order/other/finish/carfine/\(fineInfo.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
            toOperatOrder(url: urlString)
        }
    }
}
extension FineInfoController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        fineInfoCell = tableView.dequeueReusableCell(withIdentifier: "fineInfoCellID") as? FineInfoCell
        if fineInfoCell == nil {
            fineInfoCell = FineInfoCell(style: UITableViewCellStyle.default,reuseIdentifier: "fineInfoCellID")
        }
        fineInfoCell.configCellDateWithModel(fineInfo)
        return fineInfoCell!
    }
}
